/**
 * @(#)ExOnlineLoginTriggerService.java 2016年10月22日
 * Copyright(C) 2015 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import edu.hziee.common.queue.DelayExecuteBuffer;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.bto.HttpResult;
import org.jsbd.boss.domian.IPReqChannelLog;
import org.jsbd.boss.domian.IPReqLog;
import org.jsbd.boss.domian.channel.BaseResultVo;
import org.jsbd.boss.service.IChannelTaskService;
import org.jsbd.boss.service.ITaskPoolService;
import org.jsbd.boss.task.PreserveLoginTask;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.io.Serializable;

/**
 * 专门重构OnlineLoginTriggerController做的一个扩展service类，原先的service实在是太鸡巴丑了
 *
 * @author <a href="mailto:zhaihuilin@zhexinit.com" >翟惠林</a>
 * @version 1.0.0
 */

@Service
public class ExOnlineLoginTriggerService {
    public static final Logger logger = LoggerFactory.getLogger(ExOnlineLoginTriggerService.class);

    @Resource
    private IChannelTaskService channelTaskService;

    private final static String FORBIDDEN_USER_STATE = "9999";

    private final static String CHANNELS = PropertiesUtil.getValue("channels", "WH2017_001");

    @Autowired
    private DelayExecuteBuffer<IPReqLog> ipReqLogBuffer;

    @Resource
    private ITaskPoolService taskPoolService;

    @Autowired
    private DelayExecuteBuffer<IPReqChannelLog> ipReqChannelLogBuffer;

    public String getOnlineLoginHttpReq(HttpResult httpResult) {
        Integer type = httpResult.getType();
        // type==1表示请求,type==2表示结果
        if (type == 1) {
            String httqReq = null;
            BaseResultVo resultVo = getSmsTask(httpResult);
            if (resultVo != null && resultVo.isSuccess()) {
                Object data = resultVo.getData();
                if (data != null) {
                    httqReq = data.toString();
                }
            }
            logger.info("httqReq [{}]", httqReq);

            if (resultVo != null) {
                setIpRegLog(httpResult, resultVo.getStatus() + "");
            } else {
                setIpRegLog(httpResult, "-1");
            }

            if (CHANNELS.equals(httpResult.getChannelId())) {
                setIpRegChannelLog(httpResult, httqReq);
            }

            if (StringUtils.isBlank(httqReq)) {
                return "{resultCode:0}";
            }

            return httqReq;
        } else if (type == 2) {
            // 异步处理
            taskPoolService.exec(new PreserveLoginTask(httpResult));
            return "{resultCode:0}";
            //
        } else {
            logger.error("type is {}", type);
            return "{resultCode:0}";
        }

    }

    private BaseResultVo getSmsTask(HttpResult httpResult) {
        BaseResultVo resultVo = null;
        try {
            resultVo = channelTaskService.sendSms(httpResult);
        } catch (Exception e) {
            logger.error("获取短信任务失败 ", e);
        }
        return resultVo;
    }

    /**
     * setIpRegLog:设置ip注册日志
     *
     * @param httpResult
     * @author <a href="mailto:yuanmiao@zhexinit.com" >袁淼</a>
     */
    private void setIpRegLog(HttpResult httpResult, String status) {
        IPReqLog log = new IPReqLog();
        log.setIp(httpResult.getVisitIp());
        log.setLac(httpResult.getLac());
        log.setCellId(httpResult.getCellId());
        log.setLongitude(httpResult.getLongitude());
        log.setLatitude(httpResult.getLatitude());
        log.setSdkip(httpResult.getIp());
        log.setSmsCenter(httpResult.getSmsCenter());
        log.setChannelId(httpResult.getChannelId());
        //改为存sdkuuid
//        log.setAppId(httpResult.getAppId());
        log.setAppId(httpResult.getUuid());
        log.setSdkVerCode(httpResult.getSdkVerCode());
        log.setSdkVerName(httpResult.getSdkVerName());
        log.setClientVerCode(httpResult.getClientVerCode());
        log.setClientVerName(httpResult.getClientVerName());
        log.setPackageName(httpResult.getPackageName());
        log.setHsman(httpResult.getHsman());
        log.setHstype(httpResult.getHstype());
        log.setOsVer(httpResult.getOsVer());
        log.setScreenWidth(httpResult.getScreenWidth());
        log.setScreenHeight(httpResult.getScreenHeight());
        log.setCpu(httpResult.getCpu());
        log.setRamSize(httpResult.getRamSize());
        log.setRomSize(httpResult.getRomSize());
        log.setExtraSize(httpResult.getExtraSize());
        log.setImsi(httpResult.getImsi());
        log.setImei(httpResult.getImei());
        log.setNetworkType(httpResult.getNetworkType());
        log.setPhoneNum(httpResult.getPhoneNum());
        log.setMac(httpResult.getMac());
        log.setRoot(httpResult.getRoot());
        log.setNetworkSystem(httpResult.getNetworkSystem());
        log.setICCID(httpResult.getICCID());
        log.setNetworkType(httpResult.getNetworkType());
        log.setBuildId(httpResult.getBuildId());
        log.setState(status);
        if (httpResult.getBatteryInfo() != null) {
            log.setBatteryInfo(httpResult.getBatteryInfo().toString());
        }
        log.setRadioVersion(httpResult.getRadioVersion());
        ipReqLogBuffer.add(log);
    }

    private void setIpRegChannelLog(HttpResult httpResult, String httqReq) {
        IPReqChannelLog log = new IPReqChannelLog();
        log.setIp(httpResult.getVisitIp());
        log.setLac(httpResult.getLac());
        log.setCellId(httpResult.getCellId());
        log.setLongitude(httpResult.getLongitude());
        log.setLatitude(httpResult.getLatitude());
        log.setSdkip(httpResult.getIp());
        log.setSmsCenter(httpResult.getSmsCenter());
        log.setChannelId(httpResult.getChannelId());
        log.setAppId(httpResult.getAppId());
        log.setSdkVerCode(httpResult.getSdkVerCode());
        log.setSdkVerName(httpResult.getSdkVerName());
        log.setClientVerCode(httpResult.getClientVerCode());
        log.setClientVerName(httpResult.getClientVerName());
        log.setPackageName(httpResult.getPackageName());
        log.setHsman(httpResult.getHsman());
        log.setHstype(httpResult.getHstype());
        log.setOsVer(httpResult.getOsVer());
        log.setScreenWidth(httpResult.getScreenWidth());
        log.setScreenHeight(httpResult.getScreenHeight());
        log.setCpu(httpResult.getCpu());
        log.setRamSize(httpResult.getRamSize());
        log.setRomSize(httpResult.getRomSize());
        log.setExtraSize(httpResult.getExtraSize());
        log.setImsi(httpResult.getImsi());
        log.setImei(httpResult.getImei());
        log.setNetworkType(httpResult.getNetworkType());
        log.setPhoneNum(httpResult.getPhoneNum());
        log.setMac(httpResult.getMac());
        log.setRoot(httpResult.getRoot());
        log.setNetworkSystem(httpResult.getNetworkSystem());
        log.setICCID(httpResult.getICCID());
        log.setNetworkType(httpResult.getNetworkType());
        log.setBuildId(httpResult.getBuildId());
        if (StringUtils.isBlank(httqReq)) {
            log.setState("1");
        } else if (FORBIDDEN_USER_STATE.equals(httqReq)) {
            log.setState(FORBIDDEN_USER_STATE);
        } else {
            log.setState("0");
        }
        if (httpResult.getBatteryInfo() != null) {
            log.setBatteryInfo(httpResult.getBatteryInfo().toString());
        }
        log.setRadioVersion(httpResult.getRadioVersion());

        ipReqChannelLogBuffer.add(log);
    }

}
